<template>
  <div class="dist-box w-card">
    <div class="content-box">
      <div class="title-box">
        <span>下游接口管理</span>
        <el-button style="margin-left:auto;" type="primary" @click="addDist('add',null)">新增下游接口</el-button>
      </div>
      <div class="search-box">
        <div>
          <span>下游名称：</span>
          <el-input v-model="searchForm.shopName" clearable placeholder="请输入下游名称" />
        </div>
        <div>
          <span>接口状态：</span>
          <el-select style="width:100%;" v-model="searchForm.flag" clearable>
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
        <div style="width:30%;">
          <span>下单时间：</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" />
        </div>
        <el-button style="margin-left:0;" type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" />
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="shopName" label="下游名称" align="center">
            <template v-slot="scope">
              {{ scope.row.shopName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="appid" label="APPID" align="center">
            <template v-slot="scope">
              {{ scope.row.appid || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="appkey" label="APPKEY" align="center">
            <template v-slot="scope">
              {{ scope.row.appkey || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="allowIps" label="IP地址" align="center">
            <template v-slot="scope">
              {{ scope.row.allowIps || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="notifyUrl" label="回调地址" align="center">
            <template v-slot="scope">
              {{ scope.row.notifyUrl || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="flag" label="状态" align="center" width="100">
            <template v-slot="scope">
              <span :class="'state'+scope.row.flag"></span>
              {{ getStatus(scope.row.flag) }}
            </template>
          </el-table-column>
          <el-table-column prop="createUser" label="开通人" align="center">
            <template v-slot="scope">
              {{ scope.row.createUser || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="开通时间" align="center" width="170">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="operations" label="操作" align="center" width="100">
            <template v-slot="scope">
              <el-link v-show="scope.row.flag == 2" style="font-size:14px;" type="primary"
                @click="setHandle(1,[scope.row])">启用</el-link>
              <el-link v-show="scope.row.flag == 1" style="font-size:14px;" type="danger"
                @click="setHandle(2,[scope.row])">禁用</el-link>
              <el-link style="font-size:14px;margin-left:10px;" type="primary" @click="addDist('edit',scope.row)">编辑</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <div>
            <el-button :disabled="chooseList.length == 0" plain @click="setHandle(1,chooseList)">批量启用</el-button>
            <el-button :disabled="chooseList.length == 0" plain @click="setHandle(2,chooseList)">批量禁用</el-button>
          </div>
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <edit-dialog ref="editDialogRef" @dialogClose="dialogClose" />
  <confirm-pass ref="confirmPassRef" @verification="verification" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { warningDeal, getDateTime, getWindowWidthAndHeight } from '@/utils/utils'
import editDialog from './components/editDialog.vue'
import confirmPass from '@/components/confirmPass.vue'
import api from './api'
export default {
  components: {
    editDialog,
    confirmPass,
  },
  setup() {
    const editDialogRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      type: null,
      handleData: null,
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      searchForm: {},
      tableData: [],
      chooseList: [],
      statusOptions: [
        {
          value: 1,
          label: '启用',
        },
        {
          value: 2,
          label: '禁用',
        },
      ],
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        startTime: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[0].getTime() : null,
        endTime: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[1].getTime() : null,
      }
      api.apiUserQryList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleSelectionChange(e) {
      state.chooseList = e
    }

    function getStatus(value) {
      let label = ''
      state.statusOptions.map(item => {
        if (item.value == value) {
          label = item.label
        }
      })

      return label
    }

    function dialogClose() {
      getTableData()
    }

    function addDist(type, data) {
      editDialogRef.value.dialogShow(type, data)
    }

    function getAppid(list) {
      const myArr = []
      list && list.map(item => {
        item && myArr.push(item.appid)
      })
      return myArr
    }

    function setHandle(type, data) {
      state.type = type
      state.handleData = getAppid(data).join(',')
      let title = ''
      let msg = ''
      switch (type) {
        case 1:
          //  启用
          title = '下游接口启用'
          msg = '是否确定启用下游接口？'
          break
        case 2:
          // 禁用
          title = '下游接口禁用'
          msg = '是否确定禁用下游接口？'
          break
      }
      confirmPassRef.value.dialogShow(title, msg, false)
    }

    function verification() {
      switch (state.type) {
        case 1:
          //  启用
          setStatus(2, state.handleData)
          break
        case 2:
          // 禁用
          setStatus(1, state.handleData)
          break
      }
    }

    function setStatus(type, appid) {
      const obj = {
        type,
        appid,
      }
      api.apiUserState(obj).then(res => {
        if (res.code == '200') {
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 280
      getTableData()
    })

    return {
      ...toRefs(state),
      editDialogRef,
      confirmPassRef,
      searchHandle,
      resetHandle,
      handleCurrentChange,
      handleSizeChange,
      handleSelectionChange,
      getStatus,
      getDateTime,
      dialogClose,
      addDist,
      setHandle,
      verification,
    }
  }
}
</script>

<style lang="scss" scoped>
.dist-box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .content-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px;
  }
}
.title-box {
  display: flex;
  align-items: center;
  > span {
    font-size: 16px;
    font-weight: bold;
  }
}
.table-box {
  margin-top: 10px;
  .table-footer {
    margin-top: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.search-box {
  display: flex;
  align-items: center;
  gap: 20px;
  box-sizing: border-box;
  padding: 5px;
  margin-top: 10px;
  > div {
    display: flex;
    width: 25%;
    align-items: center;
    > span {
      flex-shrink: 0;
      font-size: 14px;
    }
  }
}
.state2 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #ff3939;
}
.state1 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #03b615;
}
</style>